<template>
  <div class="main">
    <hmHead class="fixd" title="品牌管理"></hmHead>
    <div class="prod" v-for="(item, index) in prodList" :key="index">
      <div class="left">{{ index + 1 }}</div>
      <div class="box">
        <div class="right">
          <div class="name">品牌名称：{{ item.name }}</div>
          <div class="ctime">添加时间：{{ item.ctime | dateFormat }}</div>
        </div>
        <van-button class="del" @click="delFn(index)" type="danger">删除品牌</van-button>
      </div>
    </div>
    <div class="btn">
        <van-field clearable v-model.trim="name" placeholder="请输入品牌名称">
            <template #button>
                <van-button type="primary" @click="addFn">添加品牌</van-button>
            </template>
        </van-field>
    </div>
  </div>
</template>

<script>
import hmHead from "@/component/hm-head.vue";
import { getprodlist } from "@/api/photos.js";
export default {
  components: {
    hmHead,
  },
  data() {
    return {
      prodList: [],
      name: "本田",
    };
  },
  mounted() {
    if (JSON.parse(localStorage.getItem("prodList"))) {
      this.prodList = JSON.parse(localStorage.getItem("prodList"));
    }else{
      this.init()
    }
  },
  methods: {
    // 封装
    init(){
      getprodlist().then((res) => {
        console.log(res);
        localStorage.setItem("prodList",JSON.stringify(res.data.message));
        this.prodList = JSON.parse(localStorage.getItem("prodList"));
      });
    },
    // 删除品牌
    delFn(index) {
        this.prodList.splice(index,1);
        localStorage.setItem("prodList",JSON.stringify(this.prodList));
        this.$toast.fail("删除品牌数据ok")
    },
    // 新增品牌
    addFn(){
        this.prodList.push({id:0,name:this.name,ctime:new Date()});
        localStorage.setItem("prodList",JSON.stringify(this.prodList));
        this.$toast.success("新增品牌成功")
    }
  },
};
</script>

<style lang="less" scoped>
.main{
    padding-bottom: 60px;
    padding-top: 50px;
    .fixd{
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
    }
    .prod {
      display: flex;
      margin: 10px;
      background-color:rgba(248,248,249,0.5);
      .left {
        width: 80px;
        height: 90px;
        line-height: 90px;
        text-align: center;
        font-size: 24px;
        font-weight: 700;
        color: #fff;
        background-color: #1989fa;
      }
      .box {
          flex: 1;
          display: flex;
          justify-content: space-between;
          align-items: center;
        .right {
          display: flex;
          flex-direction: column;
          padding: 10px;
          .name {
            font-size: 18px;
            font-weight: 700;
          }
          .ctime {
            font-size: 14px;
            margin-top: 20px;
          }
        }
        .del {
            margin-right: 5px;
        }
      }
    }
    .btn{
        z-index: 101;
        width: 94%;
        position: fixed;
        left: 0;
        bottom: 40px;
        margin: 10px;
        border: 1px solid#eee;
        /deep/ .van-cell{
            //  background-color:rgba(248,248,249,0.5);
            .van-cell__value{
                background-color: #fff;
                padding-left: 10px;
                border: 1px solid#eee;
            }
        }
    }
}
</style>